#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

int *n,i=0,j=0,total=0;

void sort()
{	int x,y,temp,t=0;
	for(x=0;x<total-1;x++)
	{
		for(y=0;y <(total-x-1);y++)
		{
			if(n[y]>n[y+1])
			{
				temp=n[y];
				n[y]=n[y+1];
				n[y+1]=temp;
			}
		}
	}
	printf("\nsorted list is :: ");
	while(t<total)
	{
		printf("%d ",n[t]);
		t=t+1;
	}
}

void append()
{
	int no=0,nwno;
	printf("\nCurrent list is :");
	while(no<total)
	{
		printf("%d ",n[no]);
		no=no+1;
	}
	n=realloc(n,sizeof(int)*(++total));
	printf("\nEnter the no. u wanna add : ");
	scanf("%d",&nwno);
	n[total-1]=nwno;
	printf("\nNew list is: ");
	no=0;
	//total=total+1;
	while(no<total)
	{
		printf("%d ",n[no]);
		no=no+1;
	}
}

void reverse()
{
	int now=0,p,q,temp;
	int half=total/2;
	for(p=0,q=total-1;p<=half,q>half;p++,q--)
	{
		temp=n[q];
		n[q]=n[p];
		n[p]=temp;
	}
	printf("The reversed list is : ");
	while(now<total)
	{
		printf("%d ",n[now]);
		now=now+1;
	}
}

void slicing()
{
	int d=0;
	int b[200];
	int j,s,z,t,k;
	int r=0;
	printf("Enter the values of index u want to slice in form[a:b]: ");
	scanf("%d:%d",&j,&k);
	if(j<0 && k<0)
	{
		for(z=total+j;z<total+k;z++)
		{
			b[d]=n[z];
			d++;
		}
	}
	else
	{
		for(s=j;s<k;s++)
		{
			b[d]=n[s];
			d++;
		}
	}
	while(r<d)
	{
		printf("%d ",b[r]);
		r++;
	}
}

void index1()
{
	int u,s,flag=1;
	printf("\nEnter the no. to know its position : ");
	scanf("%d",&u);
	for(s=0;s<total;s++)
	{
		if(n[s]==u)
		{
		flag=0;
		break;
		}
		else
		continue;
	}
	if(flag==0)
	{
	printf("The index is : %d",s);
		}
	else
	{
			printf("No. not found !!");
		}

}

void extend()
{	int total2=0,i1=0,w,v=0,i2,dtot=0;
	int n2[200];
	char ch=',';
	printf("Enter the elements of the list(separated by comma & fullstop in end) : ");
	while(ch!='.')
	{
		scanf("%d%c",&n2[i1],&ch);
		i1=i1+1;
		total2=i1;
	}
	n=realloc(n,sizeof(int)*(total+total2));
	for(w=0;w<total2;w++)
	{
		n[w+total]=n2[w];
	}
	total=total2+total;
	printf("\nNew extended list is : ");
	while(v<total)
	{
		printf("%d ",n[v]);
		v=v+1;
	}
}

void insert()
{
	int a,b,e,h,f,g=0;
	int arr[200],arr1[200];
	int de,de1,dh,le,le1;
	n=realloc(n,sizeof(int)*(++total));
	printf("Enter a no. to be inserted : ");
	scanf("%d",&a);
	printf("Enter the position where no. to b inserted : ");
	scanf("%d",&b);
	for(de=0,le=b;de<total-b,le<total;de++,le++)
	{
		arr[de]=n[le];
	}
	for(de1=0,le1=0;de1<b,le1<b;de1++,le1++)
	{
		arr1[de1]=n[le1];
	}
	for(f=0;f<de1;f++)
	{
		n[f]=arr1[f];
	}
	n[b]=a;
	for(h=b+1,dh=0;h<total+1,dh<de+1;h++,dh++)
	{
		n[h]=arr[dh];
	}
	printf("\nList after insertion is : ");
	while(g<total)
	{
		printf("%d ",n[g]);
		g=g+1;
	}
}

void count()
{
	int num,cnt=0;
	printf("\nEnter the no. u wanna count : ");
	scanf("%d",&num);
	for(i=0;i<total;i++)
	{
		if(n[i]==num)
		{
			cnt++;
		}
	}
	printf("\nThe element %d comes %d times in the list. ",num,cnt);
}

void remove1()
{
	int dw=0,j;
	int numbr;
	printf("\n Enter the no. u wanna remove : ");
	scanf("%d",&numbr);
	for(i=0;i<total;i++)
	{
		if(n[i]==numbr)
		{
			for(j=i;j<total-1;j++)
			{
				n[j]=n[j+1];
			}

		break;
		}
		else
		n[i]=n[i];
	}
	n=realloc(n,sizeof(int)*(--total));
	printf("New list is : ");
	while(dw<total)
	{
		printf("%d ",n[dw]);
		dw++;
	}
}

void pop()
{
	int addrs,dw=0;
	printf("\nEnter the index of element u wanna remove : ");
	scanf("%d",&addrs);
if(addrs<0)
{	addrs=addrs+total;
}
	for(i=0;i<total;i++)
	{
		if(i==addrs)
		{
			for(j=i;j<total-1;j++)
			{
				n[j]=n[j+1];
			}
		}
		else
		n[i]=n[i];
	}
	n=realloc(n,sizeof(int)*(--total));
	printf("New list is : ");
	while(dw<total)
	{
		printf("%d ",n[dw]);
		dw++;
	}
}

void main()
{
int m;
char ch=',';
char cr;
printf("Enter the elements of the list(separated by comma & fullstop in end) : ");
while(ch!='.')
{	n=realloc(n,sizeof(int)*(++total));
	scanf("%d%c",&n[i],&ch);
	i=i+1;
	total=i;
}
printf("\nsThe list u entered is : ");
while(j<i)
{
	printf("%d ",n[j]);
	j=j+1;
}
do
{
	printf("\n1.Sorting \n2.Append \n3.Reverse \n4.Slicing \n5.Index \n6.Extend \n7.Insert \n8.Count \n9.Remove \n10.Pop");
	printf("\n\nEnter the operation u wanna perform : ");
	scanf("%d",&m);
	switch(m)
	{
		case 1:sort();
		break;
		case 2:append();
		break;
		case 3:reverse();
		break;
		case 4:slicing();
		break;
		case 5:index1();
		break;
		case 6:extend();
		break;
		case 7:insert();
		break;
		case 8:count();
		break;
		case 9:remove1();
		break;
		case 10:pop();
		break;
				
	}
	printf("\nDo u wanna enter more (y/n): ");
	scanf("\n%c",&cr);
}
while(cr == 'y');
getchar();
}


